package zrar.panel;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.util.Observable;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import zrar.InputDataFrame;
import zrar.SetPrizeFrame;
import zrar.util.SystemUtil;

public class MenuPanel extends Observable implements ActionTouchImpl {
	private static final long serialVersionUID = 1L;

	private JPanel thisPanel = new JPanel();

	private InputDataFrame inputDataFrame;
	private SetPrizeFrame setPrizeFrame;

	private int position_x = 5;
	private int position_y = 5;

	private CardLayout cardLayout;

	private JButton menubut;
	private JPanel cardPane;
	private JPanel visiblePane;
	private JPanel hidePane;

	private JLabel input_lb;
	private JLabel prize_lb;
	private JLabel base_lb;
	private JLabel close_lb;

	private boolean visible = false;
	private static final int OPEN = 4;
	private int actionNum;

	public MenuPanel() {
		thisPanel.setLayout(null);

		ImageIcon menu_icon = SystemUtil.createImageIcon("menulogo.jpg");
		menubut = new JButton(menu_icon);
		cardPane = new JPanel();
		cardLayout = new CardLayout();
		cardPane.setLayout(cardLayout);

		visiblePane = new JPanel();
		hidePane = new JPanel();

		cardPane.add(hidePane, "hide");
		cardPane.add(visiblePane, "show");

		input_lb = new JLabel("导入名单", JLabel.CENTER);
		prize_lb = new JLabel("奖项设置", JLabel.CENTER);
		base_lb = new JLabel("基本设置", JLabel.CENTER);
		close_lb = new JLabel("退出程序", JLabel.CENTER);
		visiblePane.setLayout(new GridLayout(4, 2));
		visiblePane.add(input_lb);
		visiblePane.add(prize_lb);
		visiblePane.add(base_lb);
		visiblePane.add(close_lb);

		init();
		thisPanel.add(menubut);
		thisPanel.add(cardPane);
	}

	private void init() {
		thisPanel.setOpaque(false);
		menubut.setBounds(position_x, position_y, 41, 35);
		cardPane.setBounds(position_x, position_y + 36, 70, 90);
		cardPane.setOpaque(false);
		// visiblePane.setOpaque(false);
		hidePane.setOpaque(false);

		menubut.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {

				if (visible) {
					cardLayout.show(cardPane, "hide");
					visible = false;
				} else {
					cardLayout.show(cardPane, "show");
					visible = true;
				}

			}
		});

		input_lb.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {

				if (inputDataFrame == null) {
					inputDataFrame = new InputDataFrame();
				}
				inputDataFrame.showFrame();
			}
		});

		prize_lb.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {

				if (setPrizeFrame == null) {
					setPrizeFrame = new SetPrizeFrame();
				}
				setPrizeFrame.showFrame();
			}
		});
		base_lb.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				notifyTheObservers(OPEN);
			}
		});
		close_lb.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				SystemUtil.close_Action();
			}
		});

		menubut.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

		input_lb.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		prize_lb.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		base_lb.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		close_lb.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
	}

	public void closeMenuPanel() {
		cardLayout.show(cardPane, "hide");
		visible = false;
	}

	public JPanel getPane() {
		return thisPanel;
	}

	private void notifyTheObservers(int flag) {
		setActionNum(flag);
		this.setChanged();
		this.notifyObservers();
	}

	@Override
	public int getActionNum() {
		return actionNum;
	}

	@Override
	public void setActionNum(int actionNum) {
		this.actionNum = actionNum;

	}

}
